﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using DMS.BO.Models;
using DMS.BO.Security.Principal;
using DMS.Business;
using DMS.Entities;

namespace DMS.BO.Controllers
{
    public class ProductController : Controller
    {
        [AuthorizeAdmin(Permission = DMSPermission.SanPham_TimKiem)]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Search(ProductFilter filter)
        {
            int totalRecords;
            List<Product> lstProduct = ProductService.GetAll(filter.ID, filter.Name, filter.PageIndex, filter.PageSize,
                                                             out totalRecords);
            var modelList = new SearchResult<Product>(filter)
                                {
                                    TotalRecords = totalRecords,
                                    List = lstProduct
                                };
            return PartialView("Partial/_ProductSearchView", modelList);
        }

        [AuthorizeAdmin(Permission = DMSPermission.SanPham_ThemMoi)]
        public ActionResult Add(ProductFilter filter)
        {
            try
            {
                if (Request.HttpMethod == "GET")
                {
                    return PartialView("Partial/_ProductAddNew");
                }

                ProductService.Add(filter.ID, filter.Name);

                return Json(new CommandResult {Code = 0, Message = "Thành công"}, JsonRequestBehavior.AllowGet);
            }
            catch (Exception exception)
            {
                return Json(new CommandResult {Code = 1, Message = exception.InnerException.Message},
                            JsonRequestBehavior.AllowGet);
            }
        }

        [AuthorizeAdmin(Permission = DMSPermission.SanPham_ChinhSua)]
        public ActionResult Edit(ProductFilter filter)
        {
            try
            {
                if (Request.HttpMethod == "GET")
                {
                    Product product = ProductService.GetByID(filter.ID);
                    filter.Name = product.Name;
                    return PartialView("Partial/_ProductEdit", filter);
                }

                ProductService.Edit(filter.ID, filter.Name);

                return Json(new CommandResult {Code = 0, Message = "Thành công"}, JsonRequestBehavior.AllowGet);
            }
            catch (Exception exception)
            {
                return Json(new CommandResult {Code = 1, Message = exception.InnerException.Message},
                            JsonRequestBehavior.AllowGet);
            }
        }

        [AuthorizeAdmin(Permission = DMSPermission.SanPham_Xoa)]
        public ActionResult Delete(long id)
        {
            try
            {
                ProductService.Delete(id);

                return Json(new CommandResult {Code = 0, Message = "Thành công"}, JsonRequestBehavior.AllowGet);
            }
            catch (Exception exception)
            {
                return Json(new CommandResult {Code = 1, Message = exception.InnerException.Message},
                            JsonRequestBehavior.AllowGet);
            }
        }
    }
}